package com.gxsyh.nantie.phone.provider.service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.gxsyh.nantie.commons.dto.PageHelpPhoneDTO;
import com.gxsyh.nantie.phone.provider.api.TbInformationService;
import com.gxsyh.nantie.phone.provider.api.domain.TbInformation;
import com.gxsyh.nantie.phone.provider.mapper.TbInformationMapper;
import org.apache.dubbo.config.annotation.Service;
import org.springframework.transaction.annotation.Transactional;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.List;

/**
 * @email yom535@outlook.com
 * @author: 有民(yom535)
 * @date: 2020/2/12
 * @time: 15:57
 */
@Transactional
@Service(version = "1.0.0")
public class TbInformationServiceImpl implements TbInformationService{

    @Resource
    private TbInformationMapper tbInformationMapper;

    @Override
    public int addInformation(TbInformation tbInformation) {
        return tbInformationMapper.insert(tbInformation);
    }

    @Override
    public PageHelpPhoneDTO selectAllInformation(int pageNum) {
        PageHelper.startPage(pageNum,5,"serialNum,createTime DESC");
        PageInfo<TbInformation> pageInfo=new PageInfo<>(tbInformationMapper.selectAll());
        List<TbInformation> allInformation = pageInfo.getList();
        PageHelpPhoneDTO<TbInformation> pageHelpDTO=new PageHelpPhoneDTO<>();
        pageHelpDTO.setPages(pageInfo.getPages());
        pageHelpDTO.setTotal(pageInfo.getTotal());
        pageHelpDTO.setNextPage(pageInfo.getNextPage());
        pageHelpDTO.setData(allInformation);
        return pageHelpDTO;
    }

    @Override
    public int updateInformation(TbInformation tbInformation) {
        Example example=new Example(TbInformation.class);
        example.createCriteria().andEqualTo("id",tbInformation.getId());
        return tbInformationMapper.updateByExampleSelective(tbInformation,example);
    }

    @Override
    public int deleteInformation(Long id) {
        Example example=new Example(TbInformation.class);
        example.createCriteria().andEqualTo("id",id);
        return tbInformationMapper.deleteByExample(example);
    }

    @Override
    public int topInformation(Long id) {
        //先取消之前的置顶
        Example selectOne=new Example(TbInformation.class);
        selectOne.createCriteria().andEqualTo("serialnum",1);
        TbInformation tbInformation=new TbInformation();
        tbInformation.setSerialnum(999999L);
        tbInformationMapper.updateByExampleSelective(tbInformation,selectOne);

        //通过ID置顶
        Example example=new Example(TbInformation.class);
        example.createCriteria().andEqualTo("id",id);
        TbInformation topInformation=new TbInformation();
        topInformation.setSerialnum(1L);
        return tbInformationMapper.updateByExampleSelective(topInformation,example);
    }
}
